﻿using BLL;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public class SyncDataList
{
    public void UpLoadData(string url)
    {
        BasicBLL basicBLL = new BasicBLL();
        // int Count = basicBLL.GetxskdHCount(" where  djstate=0");//待上传销售单记录数
        int Size = 20;//一次提取数量
        int pageIndex = 1;
        bool doing = true;
        string userName = "";
        DataTable dt = new DataTable();

        DataSet dsH = new DataSet();
        DataSet dsB = new DataSet();
        int count = 0;
        while (true)
        {
            dt = basicBLL.GetpfdHData(Size, pageIndex, ref count);

            if (dt != null && dt.Rows.Count > 0)
            {
                dsH.Clear();
                dsB.Clear();

                dsH = new DataSet();
                dsB = new DataSet();
                dsH.Tables.Add(dt);//填装 表头部分
                foreach (DataRow dr in dt.Rows)
                {
                    xskdh xskdhModel = basicBLL.GetXskdhModel(dr["xsdh"].ToString());
                    userName = xskdhModel.djyh;
                    DataSet xskdbDs = basicBLL.GetxskdBDataDs("  where djlsh=" + dr["djlsh"].ToString());
                    dsB = DataSetHelper.FilledDs(dsB, xskdbDs.Tables[0]);
                }

                byte[] dtXskdh = DataSetHelper.GetDataSetSurrogateZipBYtes(dsH);

                byte[] dtXskdb = DataSetHelper.GetDataSetSurrogateZipBYtes(dsB);


                NewTransportObject transportobject = new NewTransportObject();
                transportobject.StrXskdH = dtXskdh;
                transportobject.DtXskdb = dtXskdb;
                ArrayList parms = new ArrayList();
                parms.Add(transportobject.StrXskdH);
                parms.Add(transportobject.DtXskdb);
                string reslut = DSWebService.InvokeWebService(url, "InsertYw_pfd", parms.ToArray());

                if (int.Parse(reslut) > 0)
                {
                    ArrayList parms2 = new ArrayList();
                    if (userName == null)
                        userName = "minjie";
                    parms2.Add(userName);
                    reslut = DSWebService.InvokeWebService(url, "CheckXskdh", parms2.ToArray());

                    //修改本地数据库 xskdh状态
                    int s3 = basicBLL.UpdatexskdhStatus(dt);
                }
                else if (int.Parse(reslut) == -2)
                {


                    LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "无法连接目标服务器请检查网络");
                    break;
                }
                else
                {

                    LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "数据上传失败请联系管理员");
                    break;
                }
            }
            else
            {
                break;
            }
            pageIndex++;
        }
        if (count > 0)
            LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "上传完成 总共记录数目：" + count);
    }

    public void DownloadGetYyyH(string url)
    {
        ArrayList parms = new ArrayList();
        BasicBLL cpdaHBLL = new BasicBLL();
        string reslut = "";

        parms.Add(reslut);
        reslut = DSWebService.InvokeWebService(url, "GetYyyData", parms.ToArray());

        if (reslut != "-2")
        {
            if (reslut != "")
            {
                DataTable dt = DataSetHelper.DeserializeDataTable(reslut);
                //插入数据库
                foreach (DataRow newdr in dt.Rows)
                {
                    string ygbh = newdr["ygbh"].ToString();
                    yyydaHModel model = cpdaHBLL.GetyyydaHModel(newdr);
                    if (cpdaHBLL.IsExistYyy(ygbh))
                    {
                        bool s1 = cpdaHBLL.Updatejbxx_yyydaHModel(model);
                    }
                    else
                    {
                        bool s2 = cpdaHBLL.Addjbxx_yyydaHModel(model);
                    }
                }
                LogHelper.m_CreateErrorLogTxt("营业员信息  ", "0", " 下载完成！");
            }
            else
            {
                LogHelper.m_CreateErrorLogTxt("营业员信息  ", "0", " 无数据下载！");
            }
        }
        else
        {
            // this.osProgress3.AutoProgress = false;
            // MessageBox.Show(" 无法连接目标服务器请检查网络！提取营业员信息失败 ");
        }
    }

    public void DownloadGetCpdaH(string url)
    {
        ArrayList parms = new ArrayList();
        BasicBLL cpdaHBLL = new BasicBLL();
        string reslut = "";
        string strWhere = "";
        // string strWhere2 = "";

        string startTime = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd") + " 00:00:00";
        string endTime = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";

        strWhere = "(( xgsj>='" + startTime + "' and xgsj<='" + endTime + "') or ( djsj>='" + startTime + "' and djsj<='" + endTime + "'))";




        int count = 0;
        int pageIndex = 1;
        int pageSize = 50;
        while (true)
        {
            parms.Clear();
            parms.Add(strWhere);
            parms.Add(pageSize);
            parms.Add(pageIndex);

            reslut = DSWebService.InvokeWebService(url, "GetCpdaData2", parms.ToArray());
            if (reslut != "-2" && reslut != "")
            {
                pageIndex++;
                DataTable dt = DataSetHelper.DeserializeDataTable(reslut);
                count += dt.Rows.Count;
                //插入数据库
                foreach (DataRow newdr in dt.Rows)
                {
                    string spbh = newdr["spbh"].ToString();
                    SpdaHModel model = cpdaHBLL.GetspdaHModel(newdr);
                    if (cpdaHBLL.IsExistSpbh(spbh))
                    {
                        bool s1 = cpdaHBLL.UpdatespdaHModel(model);
                    }
                    else
                    {
                        bool s2 = cpdaHBLL.AddspdaHModel(model);
                    }
                }
            }
            else
            {
                break;
            }
        }
        if (count > 0)
        {
            //this.osProgress2.AutoProgress = false;

            //MessageBox.Show(" 下载完成！ 共计下载数据！ ");
            //MessageBox.Show(" 下载完成！ 共计下载数据" + count + "条");
        }
        else
        {

            //this.osProgress2.AutoProgress = false;
            //MessageBox.Show(" 本时间段未获取到数据，可能网络原因，可能本时间段无更新数据！ ");
        }



    }



}

